<?php

/**
 * This is the model class for table "zy_card_info".
 *
 * The followings are the available columns in table 'zy_card_info':
 * @property integer $card_info_id
 * @property integer $user_id
 * @property integer $card_type
 * @property string $card_number
 * @property string $name
 * @property string $expirydate
 * @property string $card_accountname
 * @property string $card_bsbnumber
 * @property string $card_accountnumber
 * @property string $defaultpay
 * @property string $create_time
 * @property string $update_time
 */
class CardInfo extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CardInfo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_card_info';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, card_type', 'numerical', 'integerOnly'=>true),
			array('card_number,name, expirydate, card_bsbnumber, card_accountnumber', 'length', 'max'=>50),
			array('card_accountname', 'length', 'max'=>100),
			//array('card_number', 'length','min'=>16 ,'max'=>50),
			array('defaultpay', 'length', 'max'=>1),
			array('create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('card_info_id, user_id, card_type, card_number, name, expirydate, card_accountname, card_bsbnumber, card_accountnumber, defaultpay, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'card_info_id' => 'Card Info',
			'user_id' => 'User',
			'balance' => Yii::t('base','balance'),
			'card_type' => Yii::t('base','Payment Method'),
			'card_number' => Yii::t('base','Card Number'),
			'name' => Yii::t('base','Name on Card'),
			'expirydate' => Yii::t('base','Expiry Date'),
			'card_accountname' =>Yii::t('base','Account Name'),
			'card_bsbnumber' =>Yii::t('base','BSB'),
			'card_accountnumber' =>Yii::t('base','Account Number'),
			'defaultpay' => Yii::t('base','Save for Future Recharge'),
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('card_info_id',$this->card_info_id);
		$criteria->compare('user_id',yii::app()->user->id);
		$criteria->compare('card_type',$this->card_type);
		$criteria->compare('card_number',$this->card_number,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('expirydate',$this->expirydate,true);
		$criteria->compare('card_accountname',$this->card_accountname,true);
		$criteria->compare('card_bsbnumber',$this->card_bsbnumber,true);
		$criteria->compare('card_accountnumber',$this->card_accountnumber,true);
		$criteria->compare('defaultpay',$this->defaultpay,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        /**
         * 查找用户选择默认付款方式
         * @return type
         */
        public function checkboxValid(){
            $sql="select * from zy_card_info where user_id=".yii::app()->user->id;
            $result = yii::app()->db->createCommand($sql)->queryAll();
            return $result[0]['defaultpay'];
        }
        
        
        /**
         * 卡类型名称
         */
        public function paymentMethodName($card_type){
        	if($card_type == 1){
        		return Yii::t('base','Credit Card');
        	}else if($card_type == 2){
        		return Yii::t('base','Direct Debit');
        	}else if($card_type == 3){
        		return Yii::t('base','Deduct from Deposit');
        	}
        }
	/**
     * 删除信用卡信息
     */
    public function deleteCard(){
    	$id = yii::app()->user->id;
		$sql="DELETE FROM `zy_card_info` WHERE `user_id` ='".$id."'";
		$result = yii::app()->db->createCommand($sql)->execute();
	}
}